


#include "iot_gpio.h"
#include "iot_gpio_ex.h"
#include "iot_pwm.h"
#define LED_GPIO_IDX 5
#define MOTOR_GPIO_IDX 2
#define MOTOR_PWM_CHN 2
int g_motor_level;

int motor_speed(int speed){

    if(speed <0){
        speed = 0;
    }
    int level = (speed > 5) ? 5 : speed ;
    

    if(speed == 0){
        //close motor
        IoTPwmStop(MOTOR_PWM_CHN);
    }else{
        IoTPwmStart(MOTOR_PWM_CHN, level, 40000);
    }

    g_motor_level = level;

}


int motor_init()
{
    IoTGpioInit(MOTOR_GPIO_IDX);
    IoTGpioSetFunc(MOTOR_GPIO_IDX, IOT_GPIO_FUNC_GPIO_2_PWM2_OUT);
    IoTGpioSetDir(MOTOR_GPIO_IDX,IOT_GPIO_DIR_OUT);
    IoTPwmInit(MOTOR_PWM_CHN);

}